package com.api.shihua.action;

import com.api.shihua.cmd.RewardBaseService;
import com.api.shihua.entity.Reward;
import com.api.shihua.util.BaseUtil;
import com.api.shihua.util.LogTool;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.action.BaseAction;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class RewardAction extends BaseAction {
    private final static LogTool log = new LogTool("/log/zjl/",false);
    @Override
    public String execute(RequestInfo requestInfo) {

        //requestInfo.getMainTableInfo().getProperty()[0].getName();
        //requestInfo.getDetailTableInfo().getDetailTable()[0].getRow()[0].getCell()[0].getName()
        //requestInfo.getRequestManager().set
        SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentDate=sim.format(new Date()).split(" ")[0];
        String currentTime=sim.format(new Date()).split(" ")[1];
        String nd=currentDate.split("-")[0];//年度
        String yf=(Integer.parseInt(currentDate.split("-")[1])-1)+"" ;//月份
        RecordSet re=new RecordSet();
        RecordSet re1=new RecordSet();
        String requestid=requestInfo.getRequestid();
        String workflowid=requestInfo.getWorkflowid();
        String formid= BaseUtil.getFormidId(workflowid);
        log.writeLog("开始总经理 "+requestid);
        int lastopter= Util.getIntValue(requestInfo.getLastoperator(),0) ;//提交人员
        String lastopterLastname=new User(lastopter).getLastname();//提交人员姓名
        //设置第一个明细表数据
        String  lsh="";//主流程编号
        String fjlx="";//分解类型
        String sqrq="";//申请日期(主表)
        int sqdw=-1;//申请单位(主表)
        int  sqr;//申请人(主表)
        String ndjhxm="";//年度计划-项目(主表)
        double ndjhys;//年度计划-预算(主表)
        double ndjhsyed;//年度计划-剩余额度(主表)
        double  sqjlje;//申请奖励金额(主表)
        double qzjldwjezj;//其中奖励单位金额合计(主表)
        double zcldjljehj;//中层领导奖励金额合计(主表)
        Reward reward=null;
        List<Object> list=new ArrayList<Object>();
        //奖励项目名称(主表)
        String sql="select * from formtable_main_"+formid+" a,formtable_main_"+formid+"_dt1 b  where a.id=b.mainid and a.requestid='"+requestid+"'";

        re.execute(sql);
        while(false&&re.next()){
            reward=new Reward();
            //奖励项目名称(主表)
            String tsfyxr=Util.null2String(re.getString("tsfyxr")).trim();
            if("0".equals(tsfyxr)){
                //continue;
            }
            lsh=Util.null2String(re.getString("spdh")).trim(); //主流程编号
            //fjlx=Util.null2String(re.getString("fjlx")).trim();//分解类型
            sqrq=Util.null2String(re.getString("sqsj")).trim();//申请日期(主表)
            sqdw=Util.getIntValue(re.getString("sqdw"),-1);//申请单位(主表)
            sqr=Util.getIntValue(re.getString("sqr"),-1);//申请人(主表)
            ndjhxm=Util.null2String(re.getString("jlxm")).trim();//年度计划-项目(主表)
            ndjhys=Util.getDoubleValue(re.getString("ndys"),0);//年度计划-预算(主表)
            ndjhsyed=Util.getDoubleValue(re.getString("syed"),0);//年度计划-剩余额度(主表)
            sqjlje=Util.getDoubleValue(re.getString("qjje1"),0);//申请奖励金额(主表)
            qzjldwjezj=Util.getDoubleValue(re.getString("mx2hj"),0);//其中奖励单位金额合计(主表)
            zcldjljehj=Util.getDoubleValue(re.getString("mx1hj"),0);//中层领导奖励金额合计(主表)
            String jlxmmc1=Util.null2String(re.getString("jlxmmc")).trim();//项目名称
            //获取明细
            int bjldwfb=Util.getIntValue(re.getString("bjldw"),0);//被奖励单位（分部）
            int zcld=Util.getIntValue(re.getString("zcld"),0);//中层领导
            double zcldjlje=Util.getDoubleValue(re.getString("mx1hj"),0);//中层领导奖励金额
            String zcldfpbz=Util.null2String(re.getString("bz")).trim();//中层领导分配备注
            reward.setJb(0);
            reward.setNf(nd);
            reward.setYf(yf);
            reward.setLsh(lsh);
            reward.setSqrq(sqrq);
            reward.setSqdw(sqdw);
            reward.setSqr(sqr);
            reward.setNdjhxm(ndjhxm);
            reward.setNdjhys(ndjhys);
            reward.setNdjhsyed(ndjhsyed);
            reward.setSqjlje(sqjlje);
            reward.setQzjldwjezj(qzjldwjezj);
            reward.setZcldjljehj(zcldjljehj);
            reward.setJlxmmc(jlxmmc1);

            reward.setBjldwfb(bjldwfb);
            reward.setZcld(zcld);
            reward.setZcldjlje(zcldjlje);
            reward.setZcldfpbz(zcldfpbz);

            reward.setWfrequestid(requestid);
            reward.setMainrequestid(requestid);
            reward.setWfrequestmark(lsh);
            reward.setModedatacreatertype("0");
            reward.setModedatacreater("1");
            reward.setModedatacreatedate(currentDate);
            reward.setModedatacreatetime(currentTime);
            list.add(reward);

        }
        //第二个明细
       // String sql1="select * from formtable_main_"+formid+" a,formtable_main_"+formid+"_dt2 b  where a.id=b.mainid and a.requestid='"+requestid+"'";
        String sql1="select a.*,b.zcld,c.bjldw,b.jlje zcldjljede1,b.bz zczb,c.jlje,c.bz dwbz,c.qzjldwje,c.zcjlje,c.bjldwms from formtable_main_"+formid+" a , formtable_main_"+formid+"_dt2 c left join formtable_main_"+formid+"_dt1 b on c.mainid=b.mainid and c.bjldw=b.bjldw where " +
                    " a.id=c.mainid  and a.requestId='"+requestid+"'";
        log.writeLog("sql"+sql1);
        re1.execute(sql1);
        while(re1.next()){
            String tsfyxr=Util.null2String(re1.getString("tsfyxr")).trim();
            if("0".equals(tsfyxr)){
                //continue;
            }
            reward=new Reward();
            //奖励项目名称(主表)
            //nd=Util.null2String(re1.getString("lsh")).trim();
            //yf=Util.null2String(re1.getString("lsh")).trim();
            lsh=Util.null2String(re1.getString("lsh")).trim(); //主流程编号
            //fjlx=Util.null2String(re.getString("fjlx")).trim();//分解类型
            sqrq=Util.null2String(re1.getString("sqsj")).trim();//申请日期(主表)
            sqdw=Util.getIntValue(re1.getString("sqdw"),-1);//申请单位(主表)
            sqr=Util.getIntValue(re1.getString("sqr"),-1);//申请人(主表)
            ndjhxm=Util.null2String(re1.getString("jlxm")).trim();//年度计划-项目(主表)
            ndjhys=BaseUtil.getfloats(re1.getString("ndys"),2);//年度计划-预算(主表)
            ndjhsyed=BaseUtil.getfloats(re1.getString("syed"),2);//年度计划-剩余额度(主表)
            sqjlje=BaseUtil.getfloats(re1.getString("qjje1"),2);//申请奖励金额(主表)
            qzjldwjezj=BaseUtil.getfloats(re1.getString("mx2hj"),2);//其中奖励单位金额合计(主表)
            zcldjljehj=BaseUtil.getfloats(re1.getString("mx1hj"),2);//中层领导奖励金额合计(主表)
            //获取明细
            int bjldwfb=Util.getIntValue(re1.getString("bjldw"),-1);//被奖励单位（分部）
            int zcld=Util.getIntValue(re1.getString("zcld"),0);//中层领导
            double zcldjlje=BaseUtil.getfloats(re1.getString("zcldjljede1"),2);//中层领导奖励金额
            String zcldfpbz=Util.null2String(re1.getString("zczb")).trim();//中层领导分配备注

            double jljefb=BaseUtil.getfloats(re1.getString("jlje"),2);//单位奖励金额(分部)
            double qzzcldjlje=BaseUtil.getfloats(re1.getString("zcjlje"),2);//其中中层领导奖励金额
            double qzjldwje=BaseUtil.getfloats(re1.getString("qzjldwje"),2);//其中奖励单位金额
            //String dwjlbz=Util.null2String(re1.getString("bjldwms")).trim();//奖励分配备注
            String jlfpbz=Util.null2String(re1.getString("dwbz")).trim();//奖励分配备注
            String jlxmmc1=Util.null2String(re1.getString("jlxmmc")).trim();//项目名称

            reward.setJb(0);
            reward.setNf(nd);
            reward.setYf(yf);
            reward.setLsh(lsh);
            reward.setSqrq(sqrq);
            reward.setSqdw(sqdw);
            reward.setSqr(sqr);
            reward.setNdjhxm(ndjhxm);
            reward.setNdjhys(ndjhys);
            reward.setNdjhsyed(ndjhsyed);
            reward.setSqjlje(sqjlje);
            reward.setQzjldwjezj(qzjldwjezj);
            reward.setZcldjljehj(zcldjljehj);
            reward.setJlxmmc(jlxmmc1);

            reward.setBjldwfb(bjldwfb);
            reward.setZcld(zcld);
            reward.setZcldjlje(zcldjlje);
            reward.setZcldfpbz(zcldfpbz);


            reward.setJljefb(jljefb);
            reward.setQzzcldjlje(qzzcldjlje);
            reward.setQzjldwje(qzjldwje);
            //reward.setDwjlbz(dwjlbz);
            reward.setJlfpbz(jlfpbz);
            reward.setWfrequestid(requestid);
            reward.setMainrequestid(requestid);
            reward.setWfrequestmark(lsh);
            reward.setModedatacreatertype("0");
            reward.setModedatacreater("1");
            reward.setModedatacreatedate(currentDate);
            reward.setModedatacreatetime(currentTime);
            list.add(reward);
        }
        log.writeLog(list.toString());
        String mess="";
        if(list.size()>0){
            RewardBaseService RewardBaseService=new RewardBaseService();
            mess=Util.null2String(RewardBaseService.getObjMapList(list,null)) ;
        }
        if(!"".equals(mess)){
            requestInfo.getRequestManager().setMessageid("10001");
            requestInfo.getRequestManager().setMessage("10001");//提醒信息id
            requestInfo.getRequestManager().setMessagecontent("写入建模失败：请联系管理员"+mess);//提醒信息内容
            return Action.FAILURE_AND_CONTINUE;
        }else{
            re1.execute("update formtable_main_"+formid+" set tsfyxr='0' where requestid='"+requestid+"'");
            updModePimion(requestid,46);
        }
        return super.execute(requestInfo);
    }
    /*
    更新建模权限
     */
    public void updModePimion(String wfrequestid,int modelid){
        RecordSet re=new RecordSet();
        re.execute("select * from uf_zjljlhzbb where wfrequestid='"+wfrequestid+"' ");
        while(re.next()){
            int dataid=re.getInt("id");
            ModeRightInfo mri=new ModeRightInfo();
            mri.editModeDataShare(1,modelid , dataid);
        }
    }
}
